package com.qf.blog.system.controller;

import com.qf.blog.core.utils.R;
import com.qf.blog.system.service.impl.SysLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService sysLoginService;

    @PostMapping("/login")
    public R login(@RequestBody Map<String, String> param) { // 把前端传递的JSON数据全部的转入到Map中

        // 1.获取登录的信息
        String username = param.get("username");
        String password = param.get("password");

        // 2.非空判断
        if (ObjectUtils.isEmpty(username)) {
            return R.error("用户名不能为空");
        }
        if (ObjectUtils.isEmpty(password)) {
            return R.error("密码不能为空");
        }

        // 3.实现登录
        String token = sysLoginService.login(username, password);

        // 4.把token返回给客户端(令牌给了客户端，同时令牌对应的登录信息在redis保存了)
        return R.ok().put("token",token);
    }
}
